import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login'
const Login = () => import("../components/Login")
const Home = () => import('../components/Home')
const Welcome = ()=>import('../components/Welcome')
const User = ()=>import('../components/User')
const Roles = ()=>import('../components/Roles')
const Rights = ()=>import('../components/Rights')
const Goods = ()=>import('../components/Goods')
const Params=()=>import('../components/Params')
const Categories = ()=>import('../components/Categories')
const Orders =()=>import('../components/Orders')
const Reports = ()=>import('../components/Reports')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login', component: Login
  },
  {
    path: '/home', component: Home,
    redirect:'/welcome',
    children:[
      {path: '/welcome', component:Welcome},
      {path: '/users', component:User},
      {path: '/roles', component:Roles},
      {path:'/rights',component:Rights},
      {path:'/goods',component:Goods},
      {path:'/params',component:Params},
      {path:'/categories',component:Categories},
      {path:'/orders',component:Orders},
      {path:'/reports',component:Reports}
     
      
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  var token = window.sessionStorage.getItem('token')

  if (to.path != '/login') {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
  next()
})

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {

return originalPush.call(this, location).catch(err => err)

}
export default router
